package com.embhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.embhealth.dao.PermissionDao;
import com.embhealth.dao.RoleDao;
import com.embhealth.dao.UserDao;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.pojo.Permission;
import com.embhealth.pojo.Role;
import com.embhealth.pojo.User;
import com.embhealth.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Override
    //根据用户名查询数据库获取用户信息和关联的角色信息以及角色关联的权限信息
    public User findByUsername(String username) {
        //先查询用户基本信息
        User user = userDao.findByUsername(username);
        if (user==null){
            return null;
        }
        Integer userId=user.getId();
        //根据用户id查询对应角色
        Set<Role> roles = roleDao.findByUserId(userId);
        user.setRoles(roles);
        for (Role role : roles) {
            Integer id = role.getId();
            //根据角色id查询权限
            Set<Permission> byRoleId = permissionDao.findByRoleId(id);
            role.setPermissions(byRoleId);
        }
        user.setRoles(roles);
        return user;
    }

    @Override
    public PageResult findAllUser(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.findAllUser(queryString);
        long total = page.getTotal();
        List<User> result = page.getResult();
        return new PageResult(total,result);
    }
    @Override
    public void add(Integer id, User user) {
        //添加用户信息
        String password = user.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(password);
        user.setPassword(encode);
        userDao.addUser(user);
        //添加关联关系
        userDao.addRole4User(user.getId(),id);
    }
    @Override
    public Map<String,Object> findById(Integer id){
        //查询User信息
        User user = userDao.findById(id);
        // TODO 封装对应的页面数据
        Map<String,Object> map=new HashMap<>();
        map.put("id",user.getId());
        map.put("username",user.getUsername());
        map.put("password","重置密码");
        map.put("telephone",user.getTelephone());
        map.put("station",user.getStation());
        map.put("remark",user.getRemark());
        map.put("formDataRoleid",user.getRole().getId());
        return map;
    }
    @Override
    public void editUser(Integer rid,Map<String,Object> map){
        //先删除原有的关联关系
        userDao.deleteUserRole((Integer) map.get("id"));
        //修改用户
        userDao.editUser(map);
        //授权
        userDao.addRole4User(((Integer)map.get("id")),rid);
    }
    @Override
    public void delete(Integer id){
        //先删除级联关系
        userDao.deleteUserRole(id);
        //再删除用户
        userDao.deleteUser(id);
    }
}
